home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro30 / chap14.txt < prev    next >
Text File  |  1991-02-04  |  33KB  |  708 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 14
  5.                                       ENCAPSULATION & INHERITANCE
  6.  
  7.  
  8. Encapsulation is the cornerstone upon which object oriented
  9. programming is built, and without which it would not exist.  We
  10. will cover the topic of encapsulation in this chapter in enough
  11. depth to illustrate its use and what it can do for you in software
  12. development.  Because there are many new terms in this chapter, you
  13. could very easily become intimidated, and wish to simply give up
  14. on this new topic.  You can be assured that the time spent studying
  15. encapsulation will be greatly rewarded as you apply this new
  16. technique in your software development efforts.
  17.  
  18. Object oriented programming is not a panacea to solve all of your
  19. software problems, but it is a new and improved way of programming. 
  20. In fact it is really more of a software packaging technology than
  21. a new method of programming.  You will find that your software will
  22. be easier to write and debug as you gain experience using this new
  23. packaging method.  Like any new endeavor however, it will require
  24. some effort on your part to master these concepts.
  25.  
  26. If you are using an older version of TURBO Pascal you will not be
  27. able to compile and execute the example programs in this chapter. 
  28. Versions 5.5 and newer can be used, and as mentioned earlier, it
  29. would be worth your effort to upgrade to a newer version so you can
  30. learn these techniques to improve the quality of your programs.
  31.  
  32.  
  33. OUR FIRST ENCAPSULATION
  34. _________________________________________________________________
  35.  
  36. The example program named ENCAP1.PAS contains our  ==============
  37. first example of encapsulation.  In order to keep    ENCAP1.PAS
  38. it easy to understand, it was kept very short.     ==============
  39. This results in a program that does not illustrate
  40. the advantage of using object oriented
  41. programming, but it does give us a start in the right direction. 
  42. With this in mind, load ENCAP1.PAS and we will study the code
  43. contained in it.
  44.  
  45. Line 5 has our first new reserved word, object.  This is used in
  46. much the same way that the reserved word record is used, but it has
  47. a much different meaning.  An object is permitted to have not only
  48. data embedded within it, but also procedures, functions, and
  49. constructors.  Constructors will be described in detail later. 
  50. Since data plus procedures and functions can be grouped together
  51. in this fashion, the object is said to be encapsulated.  An object
  52. is therefore a group of related data and the subprograms that
  53. operate on that data, all entities being very closely coupled
  54. together.
  55.  
  56.  
  57.  
  58.                                                         page 14-1
  59.  
  60.                        Chapter 14 - Encapsulation and Inheritance
  61.  
  62. WHAT IS A METHOD?
  63. _________________________________________________________________
  64.  
  65. A method is a term used with object oriented programming, and for
  66. the time being we will simply say that a method is either a
  67. function or a procedure (including a constructor).  A method is
  68. therefore a method for doing an operation on some data.  Lines 8
  69. through 10 are method headers and give the pattern for all calls
  70. to these methods which can be used by the compiler to check for the
  71. correct number and types of parameters.
  72.  
  73. Once again, we promise to discuss the constructor soon.  For the
  74. time being, simply think of it as another procedure.
  75.  
  76. The entire object type definition is given in lines 5 through 11. 
  77. This object contains two variables named length and width, each of
  78. type integer, and three methods which can be used to operate on the
  79. two variables.  In the same manner that the definition of a type
  80. in Pascal does not actually give you a variable to use, only a
  81. pattern, the definition of an object type does not give you an
  82. object.  We will declare the objects when we get to line 30 of this
  83. program.
  84.  
  85. You will note that we are already using new terminology, but this
  86. is necessary.  The field of object oriented programming has its own
  87. vocabulary and in order for you to understand technical articles
  88. in this field, you must begin now to learn the new terminology. 
  89. It won't be too long until you feel somewhat comfortable with it.
  90.  
  91.  
  92. THE METHOD IMPLEMENTATION
  93. _________________________________________________________________
  94.  
  95. The object type definition describes in detail what we can do with
  96. the object but we must now describe what actions will take place
  97. when each of the methods is called.  The implementation for each
  98. method will define the operations for that method and are defined
  99. in lines 13 through 28 of this example program.  The only thing
  100. that is really different about these methods is the way their
  101. headers are defined, namely the inclusion, in the header, of the
  102. object type name Box dotted to the method name.  This is required,
  103. but we will wait until the next example program to define why it
  104. is needed.
  105.  
  106. The observant student will also notice that we are referring to the
  107. object variables within the methods of that object without the
  108. object name dotted to the variable name.  This is because the
  109. implied object name is automatically "with"ed to the variable names
  110. within the method implementations, allowing the variables to be
  111. directly referred to within the objects because of the definition
  112. of object oriented programming.  It should be obvious that any
  113. mathematics or logical operations can be done within the
  114. implementations of the methods.  In fact, you can perform any legal
  115. Pascal operations within the methods, just like you can in any
  116.  
  117.                                                         page 14-2
  118.  
  119.                        Chapter 14 - Encapsulation and Inheritance
  120.  
  121. Pascal function or procedure.  Very short operations were selected
  122. here because we wish to illustrate the interfaces to the methods
  123. at this point in the tutorial.
  124.  
  125.  
  126. AN INSTANCE OF AN OBJECT
  127. _________________________________________________________________
  128.  
  129. We need another new term at this point.  When we use the object
  130. type to declare variables of that type as we do in line 30, we are
  131. creating instances of that object type.  An instance is like a
  132. variable in conventional Pascal (non object oriented), except that
  133. it can do more and has some very interesting properties that a
  134. simple variable does not have.  In line 30 we have created three
  135. instances of the object type named Box and each has two simple
  136. variables associated with it.  Three methods are available which
  137. can be called to operate on these variables.  We therefore have
  138. three objects named Small, Medium, and Large.  In order to
  139. initialize the values stored within the objects we call the three
  140. objects in lines 34 through 36 to store values in their internal
  141. variables by dotting the name of the object to the name of the
  142. method we wish to call.  You will note that this looks like the
  143. same technique we use to refer to the fields of a record.  We
  144. display the area of the three boxes in lines 38 through 40 using
  145. the same technique used to initialize the values stored, and the
  146. program is complete.
  147.  
  148. We seem to have accomplished very little with this program that we
  149. could not have more easily accomplished with an even shorter
  150. standard Pascal program, and that is true. This program is only
  151. meant to introduce some of the mechanics of object oriented
  152. programming.  Additional programs will be used to illustrate some
  153. of the uses of this new technique.
  154.  
  155.  
  156.  
  157. NEW TERMINOLOGY
  158. _________________________________________________________________
  159.  
  160. You may note that we switched terminology halfway through the above
  161. paragraphs.  We began by referring to the object types as object
  162. types and calling the variables declared in line 30 instances. 
  163. Later we began calling the instances objects.  In this tutorial we
  164. will refer to the types as object types and the variables either
  165. as objects or instances.  This terminology is consistent with
  166. current practice and should help you learn the new terminology.
  167.  
  168. Another very important point is the fact that we pass a message to
  169. a method rather than call a subprogram as in conventional Pascal. 
  170. The difference is rather subtle, but there really is a difference
  171. as we will see a little later in this tutorial.
  172.  
  173.  
  174.  
  175.  
  176.                                                         page 14-3
  177.  
  178.                        Chapter 14 - Encapsulation and Inheritance
  179.  
  180. WHAT DID WE ACCOMPLISH?
  181. _________________________________________________________________
  182.  
  183. In this program we defined an object type, then declared several
  184. instances of that type, one of which was named Small.  The object
  185. named Small has two internal variables that should only be accessed
  186. via its methods, so we will refer to them as private data points. 
  187. Actually, they should be unavailable to any user outside of the
  188. method implementations but Borland chose not to make them private
  189. in version 5.5.  It is up to you to discipline yourself to not
  190. refer to them directly.  (TURBO Pascal version 6.0 has a way to
  191. make the data private.  We will study this in the next example
  192. program.)  The proper way to use the object is to send a message
  193. to the object telling it to do something to itself.  In the case
  194. of the Init method, we are telling it to store the two values in
  195. its private variables named length and width, and in the case of
  196. the Get_Area method, we are telling it to give us the product of
  197. its own internally stored width and length which we can then print
  198. out.
  199.  
  200. Remember that in the beginning of this chapter we said that object
  201. oriented programming is a code packaging technique.  That should
  202. help to explain some of the strange things we did in this program. 
  203. As we continue through the example programs, we will see that
  204. everything here was done for a reason and you will eventually learn
  205. to use and prefer object oriented programming methods over the old
  206. familiar procedural programming method you have been using.
  207.  
  208. Be sure to compile and execute this program to see if it does what
  209. the comments say it will do.
  210.  
  211.  
  212. DATA & CODE PROTECTION
  213. _________________________________________________________________
  214.  
  215. The data and methods are protected from outside influence because
  216. they are packaged together within an object.  Of even more
  217. importance is the fact that because they were to be packaged
  218. together, they were probably carefully planned and designed
  219. together during the design stage.  This would probably result in
  220. a more understandable program.  The object keeps the data and
  221. methods together and keeps them working in close synchronization.
  222.  
  223. An object type is sometimes referred to as an abstract data type
  224. in the technical literature discussing object oriented programming.
  225.  
  226.  
  227. MORE ENCAPSULATION
  228. _________________________________________________________________
  229.  
  230. The example program named ENCAP2.PAS uses most of the same
  231. techniques as the last program but this is much more meaningful
  232. since it illustrates one of the simplest advantages of using object
  233. oriented programming.
  234.  
  235.                                                         page 14-4
  236.  
  237.                        Chapter 14 - Encapsulation and Inheritance
  238.  
  239.  
  240. In this program, we define two object types in     ==============
  241. lines 5 through 21, Box and Pole.  Each has its      ENCAP2.PAS
  242. own unique kinds of variables associated with it,  ==============
  243. and each has three methods that can be used with
  244. these kinds of data.  The method definitions in lines 35 and 46
  245. clearly illustrate why the object name must be associated with the
  246. method name in the method implementation.  This allows you to use
  247. the same method name in more than one object definition.  In
  248. addition to the two method definitions named Set_Data given here,
  249. we could also define and use another procedure with the name
  250. Set_Data that was a normal Pascal procedure just like any others
  251. we have used in prior chapters of this tutorial.  Using the same
  252. method name in several places is often referred to as name
  253. overloading in object oriented programming terminology.
  254.  
  255. You will note that in lines 5 through 21 we define the object types
  256. which define what each object will do.  In lines 23 through 55 we
  257. define the method implementations which define how we do it.  It
  258. is assumed that you know enough Pascal at this point to understand
  259. what each method does, so nothing more will be said about the
  260. details of this program except for the private types.
  261.  
  262.  
  263.  
  264. THE PRIVATE TYPE
  265. _________________________________________________________________
  266.  
  267. Borland added the private type to version 6.0, but it is not as
  268. flexible as it could be.  You will notice that the order of
  269. declarations within the objects is significantly different in this
  270. program.  Within an object declaration, you are permitted to have
  271. public variables and methods and private variables and methods, but
  272. you are required to list all public entities first.  The reserved
  273. word private is then used with all private entities following its
  274. use.  Within each section, the variables must come first, followed
  275. by the methods.
  276.  
  277. Even though the variables are listed as private in these classes,
  278. they are still available to the main program because they are in
  279. the same Pascal unit.  If the objects were placed in a different
  280. unit as discussed in chapter 13, the variables defined as private
  281. would be unavailable in the calling program.  If you are using
  282. TURBO Pascal 5.5, you will have to remove the word private and
  283. rearrange the variables to put them ahead of the methods in the two
  284. object definitions.
  285.  
  286. In lines 57 and 58, we declare several objects of the defined
  287. object types and use some of them in the main program.  We can
  288. finally illustrate one of the biggest advantages of object oriented
  289. programming.
  290.  
  291. We should all agree that it would be silly and meaningless to
  292. multiply the height of one of the poles by the width of a box.  If
  293.  
  294.                                                         page 14-5
  295.  
  296.                        Chapter 14 - Encapsulation and Inheritance
  297.  
  298. we were using standard procedural programming with all variables
  299. defined globally, it would be a simple matter to accidentally write
  300. height*width and print out the result thinking we had a meaningful
  301. answer.  By encapsulating the data within the objects, we would
  302. have to really work at it to get that meaningless answer because
  303. the system itself would prevent us from accidentally using the
  304. wrong data.  This is true only if we have agreed not to use any of
  305. the data directly but to do all data access through the available
  306. methods.
  307.  
  308. Encapsulation is a form of information hiding, but TURBO Pascal has
  309. a rather weak form of it because, as mentioned earlier, Borland
  310. chose not to make the variables within the object private in
  311. version 5.5.  In TURBO Pascal 6.0 the variables can be defined as
  312. private variables as mentioned earlier, and are therefore
  313. unaccessible outside of the unit in which they are declared.  The
  314. client would be forced to use only the methods provided by the
  315. author of the object to access the contained data.  This is true
  316. information hiding and adds some degree of protection to the
  317. internal data.
  318.  
  319. If you are using TURBO Pascal 5.5, it is up to you to never refer
  320. to the data within the object directly as stated by Borland in the
  321. OOP Guide included with the compiler.  Even if you are using
  322. version 6.0, you must still discipline yourself to not refer to the
  323. private data within the defining unit.
  324.  
  325. The careful student will notice that since all data is carefully
  326. tied up within the objects, inadvertent mixing of the wrong data
  327. is impossible provided a few simple rules are followed as discussed
  328. above.  Once again, this is such a small program that it is
  329. difficult to see the advantage of going to all of this trouble. 
  330. In a larger program, once the objects are completed, it is a simple
  331. matter to use them knowing that they are debugged and working.
  332.  
  333. After the data are all printed out, some of the variables are
  334. changed in lines 80 through 82, and the same output statements are
  335. used to reprint the same data so you can observe the changes.
  336.  
  337.  
  338.  
  339. A FEW RULES ARE NEEDED
  340. _________________________________________________________________
  341.  
  342. As with any new topic, there are a few rules we must follow to use
  343. this new technique.  The public variables must all be declared
  344. first in the object followed by the public method definitions.  The
  345. reserved word private is then declared followed by the private
  346. variables and finally the private methods.  The names of all
  347. variables within an object must be unique and may not be repeated
  348. as the names of any of the formal variables in any of the methods. 
  349. Thus length, width, len, and wid must be unique as used in lines
  350. 6, 7, 10, and 11.  The names of formal variables may be reused in
  351. other methods however, as illustrated in lines 6 and 7, and all
  352.  
  353.                                                         page 14-6
  354.  
  355.                        Chapter 14 - Encapsulation and Inheritance
  356.  
  357. names may be reused in another object.  It should be obvious that
  358. all object type names must be unique within a given file and all
  359. objects must have unique names.
  360.  
  361. All of the above rules are obvious if you spend a little time
  362. thinking about them.  They should therefore not be a stumbling
  363. block to anyone with some procedural programming experience.
  364.  
  365.  
  366. WHAT IS A CONSTRUCTOR?
  367. _________________________________________________________________
  368.  
  369. It is time to keep our promise and define just what a constructor
  370. is.  In this present context, that of simple objects, the
  371. constructor does very little for us, but we will include one for
  372. nearly every object to illustrate its use.  The constructor can be
  373. named anything desired but it would be best to stick with the
  374. convention and name every constructor Init as suggested by Borland. 
  375. The constructor is used to initialize all values within an object
  376. and do any other setup that must be done to use an object.  The
  377. constructor should be called once for every declared object.  When
  378. we get to the topic of virtual functions, constructors will be
  379. absolutely required for every object, but for the simple objects
  380. we are using here, they are optional.
  381.  
  382. It would be best to include a constructor in every object type,
  383. define the constructor to initialize all variables within the
  384. object, and call the constructor once for each instance of the
  385. object type.  Until we get to virtual methods, none of this is
  386. required, but it would be good practice to get in the habit of
  387. doing it.
  388.  
  389.  
  390. WHAT IS A DESTRUCTOR?
  391. _________________________________________________________________
  392.  
  393. A destructor is another method that can be used for cleanup when
  394. you are finished with an object.  It is usually used in conjunction
  395. with dynamic allocation to assure that all dynamically allocated
  396. fields associated with the object are deallocated prior to leaving
  397. the scope of the object.  A destructor is not illustrated in this
  398. tutorial but it should be easy for you to define and use one when
  399. you have a need for one.
  400.  
  401.  
  402.  
  403. OUR FIRST INHERITANCE
  404. _________________________________________________________________
  405.  
  406. Load the example program named INHERIT1.PAS for    ==============
  407. our first example of a program with inheritance.    INHERIT1.PAS
  408. As always, our first encounter with this new topic ==============
  409. will be very simple.
  410.  
  411.  
  412.                                                         page 14-7
  413.  
  414.                        Chapter 14 - Encapsulation and Inheritance
  415.  
  416. In lines 7 through 14 we define a simple object type defining a
  417. vehicle and a few characteristics about the vehicle.  We have the
  418. ability to store a few values and read them out in several ways. 
  419. Of course, most of the interest is in the interfaces, so the
  420. implementations are purposely kept very small.
  421.  
  422. In lines 17 through 35, we declare two additional object types that
  423. use the Vehicle type as a base for the new types as indicated by
  424. the previously defined name Vehicle in parentheses in the object
  425. definitions in lines 17 and 26.  The Vehicle object type is said
  426. to be the ancestor type and the two new object types are called
  427. descendant types.  The descendant types inherit some information
  428. from the ancestor types according to well defined rules.  The
  429. variables in the ancestor type are all included within the
  430. descendant types and are available in objects of the descendant
  431. types just as if they had been defined within the descendant types. 
  432. For that reason, all variable names must be unique within the
  433. ancestor type and within each of the descendant types.  A name can
  434. be reused in one or more descendants however, as is illustrated in
  435. lines 18 and 27 where the variable name Passenger_Load is used in
  436. both object types.
  437.  
  438. The method names from the ancestor object types can be repeated in
  439. the descendant object types but this has the effect of overriding
  440. the method of the same name in the ancestor making the ancestor
  441. method unavailable for use in objects of the descendant types. 
  442. Objects instantiated of the type Car therefore, have the three
  443. methods available in lines 11 through 13 of the ancestor type, the
  444. constructor in line 19 which overrides the constructor in line 10
  445. of the ancestor type, and the function given in line 22.  This
  446. object therefore has five different methods to perform its required
  447. operations.
  448.  
  449. Objects of type Truck have five methods available also, the two in
  450. lines 11 and 12 and the one in line 33.  The two in lines 29 and
  451. 34 of the descendant overrides the two in lines 10 and 13 of the
  452. ancestor object type.
  453.  
  454. You should note that even though some of the methods were
  455. overridden in the descendant object type, they do not affect the
  456. ancestor, and instances of the Vehicle type have two variables and
  457. four methods available.
  458.  
  459. In effect we have an object hierarchy which can be extended to as
  460. many levels as necessary to complete the task at hand.  The most
  461. important part of object oriented programming is the definition of
  462. the objects in a meaningful manner, but it is not something you
  463. will learn to do overnight.  It will take a great deal of practice
  464. until you can see the objects in any given project in such a way
  465. that a clear solution can be found.  I was somewhat intimidated by
  466. the clever examples found in a classic text on object oriented
  467. programming until I talked to a man that had shared an office with
  468. the author at the time he was writing that particular book.  I
  469. learned that what was finally put in the book was at least the
  470.  
  471.                                                         page 14-8
  472.  
  473.                        Chapter 14 - Encapsulation and Inheritance
  474.  
  475. fourth iteration of each problem and in some cases the seventh
  476. before he finally arrived at a good solution.  We will have more
  477. to say about this topic as we progress through this tutorial.
  478.  
  479.  
  480. HOW DO WE USE THE OBJECTS?
  481. _________________________________________________________________
  482.  
  483. The implementations of the methods are given in lines 39 through
  484. 92 and should be self explanatory, except for a few notable
  485. exceptions.  You will note that in line 81 we send a message to the
  486. Vehicle.Init method to initialize some data.  A change to the
  487. Vehicle type will be reflected in the Truck type also because of
  488. this call.  In lines 64 and 65 we are using some inherited
  489. variables just as if they had been defined as part of the
  490. descendant object types.
  491.  
  492. In lines 96 through 98 we instantiate one of each and send a
  493. message to their constructors in lines 102 through 104 then print
  494. out a few of the stored values.
  495.  
  496. Lines 113 through 116 are repeated in lines 120 through 123 where
  497. they are placed within a with section to illustrate that the with
  498. can be used for the calls to the methods in the same manner that
  499. it is used for accessing the fields of a record.  Any other details
  500. of this program can be gleaned by the diligent student.  Be sure
  501. to compile and execute this program so you can verify the given
  502. result.
  503.  
  504.  
  505. WHY USE INHERITANCE?
  506. _________________________________________________________________
  507.  
  508. Once you have an object that is completely debugged and working,
  509. it is possible that it can be reused on your next project.  If you
  510. cannot use it exactly as is, but are required to make a change to
  511. it, you have two choices.  You can modify the existing code and
  512. hope you don't introduce any analysis or coding errors, or you can
  513. inherit it into a new object and add code around the completely
  514. debugged object without actually modifying the original code. 
  515. Several studies have shown that modifying existing code lead to a
  516. high probability of introducing errors into the modified code that
  517. are difficult to find because you are not as familiar with the code
  518. as you should be.  Adding a few methods to an existing object,
  519. however, is not nearly as error prone and is the preferred method.
  520.  
  521.  
  522.  
  523. AN OBJECT IN A UNIT
  524. _________________________________________________________________
  525.  
  526. Load the example program named VEHICLES.PAS for an example of the
  527. proper way to package the object so it can be conveniently reused
  528. for another project.
  529.  
  530.                                                         page 14-9
  531.  
  532.                        Chapter 14 - Encapsulation and Inheritance
  533.  
  534.  
  535. The object type definition is given in the public  ==============
  536. part of the unit so it is available to any Pascal   VEHICLES.PAS
  537. program which needs to use it.  The implementation ==============
  538. of the methods are hidden in the implementation
  539. part of the unit where they are not directly available to any
  540. calling program.  Note that it is also possible to define a few
  541. local methods within the implementation for use only within the
  542. implementation but none are illustrated here.  There is no body to
  543. this unit, only the end statement in line 39 so there is no
  544. initialization code to be executed during loading.  It would be
  545. perfectly legal to include an initialization body, but even if you
  546. do, you should be sure to include a constructor to be called once
  547. for each object.  This is to prepare you for the use of virtual
  548. functions which we will study in the next chapter.
  549.  
  550. Referring back to the discussion of the second example program in
  551. this chapter, you will find the means necessary to make the
  552. variables truly private in this program.  If you move the two
  553. variables to a location just after the end of the methods, and add
  554. the reserved word private just before the two variables, they will
  555. be truly private and unavailable for direct modification outside
  556. of this unit.  If you are using TURBO Pascal 6.0, you should make
  557. this modification and try to access the variables directly in the
  558. calling program which will be discussed next.
  559. You must compile this unit to disk so it can be used with the rest
  560. of the example programs in this chapter.
  561.  
  562.  
  563.  
  564. ANOTHER OBJECT IN A UNIT
  565. _________________________________________________________________
  566.  
  567. The example program named CARTRUCK.PAS continues ================
  568. the new packaging scheme by including two          CARTRUCK.PAS
  569. descendant object types in its interface after   ================
  570. telling the system that it uses the Vehicles unit
  571. in line 6.
  572.  
  573. The remainder of this unit is constructed just like the last one
  574. so nothing more needs to be said about it.  Be sure to compile this
  575. unit to disk so it will be available for use with the next example
  576. program.
  577.  
  578. Note that this unit could have been further divided into two
  579. separate units, one for each object type, but it was felt that it
  580. was important to illustrate that several can be combined in this
  581. manner if desired.  In like manner, the last unit could have been
  582. combined with this unit, but once again, it was desired to
  583. illustrate the generality of program decomposition and packaging.
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                                        page 14-10
  590.  
  591.                        Chapter 14 - Encapsulation and Inheritance
  592.  
  593. USING THE OBJECTS DEFINED IN UNITS
  594. _________________________________________________________________
  595.  
  596. Load the program named INHERIT2.PAS for a program ================
  597. that uses the units of the last two example         INHERIT2.PAS
  598. programs and is identical to the program named    ================
  599. INHERIT1.PAS.
  600.  
  601. The only difference in these two programs is in the way the code
  602. was packaged.  The second way is much more general and more
  603. conducive to good software engineering practices because it allows
  604. separate development of each of the three program units.  Each can
  605. be refined independently of the other two and the overall package
  606. can be simpler to debug and maintain.  It should be clear that any
  607. changes to the Car object, for example, will be localized to that
  608. single unit and not scattered all over the software terrain.
  609.  
  610.  
  611. AN ARRAY AND A POINTER
  612. _________________________________________________________________
  613.  
  614. Examine the example program named INHERIT3.PAS   ================
  615. for an example of the use of a pointer to an       INHERIT3.PAS
  616. object and the use of an array of objects.       ================
  617.  
  618. This program is nearly identical to INHERIT2.PAS
  619. except for the addition of an array of Car type objects named
  620. Sedan[1] to Sedan[3], and the definition of a pointer to the Truck
  621. type object named Semi_Point.  Lines 16 and 17 illustrate the
  622. initialization of the array of Sedan, and lines 23 through 26
  623. illustrates its use when the data is printed out.  An object is
  624. dynamically allocated in line 18 and it is then initialized in the
  625. next line.  Its use is illustrated in lines 28 through 40 and it
  626. is deallocated in line 41.
  627.  
  628. TURBO Pascal 5.5 and newer, have an extension to the New procedure
  629. allowing the dynamic allocation and the initialization to take
  630. place in the same procedure call.  The line;
  631.  
  632.      New(Semi_Point, Init(1, 25000.0, 18, 5000.0));
  633.  
  634. can be used to replace lines 18 and 19 in this program if you
  635. desire to do so.
  636.  
  637. This program should illustrate that objects can be used with arrays
  638. and pointers in the same manner as a record.  Be sure to compile
  639. and execute this program.
  640.  
  641.  
  642. WHAT IS MULTIPLE INHERITANCE?
  643. _________________________________________________________________
  644.  
  645. Multiple inheritance allows the programmer to inherit data and
  646. methods from two or more ancestor objects.  When this is done
  647.  
  648.                                                        page 14-11
  649.  
  650.                        Chapter 14 - Encapsulation and Inheritance
  651.  
  652. however, there is a real problem if there are two variables or
  653. methods of the same name and it is up to the programmer to somehow
  654. define which will be used by the descendent.  Some object oriented
  655. programming languages allow multiple inheritance, but most do not. 
  656. TURBO Pascal has no provision for multiple inheritance, and Borland
  657. has made no indication at this time whether future versions will
  658. permit it.
  659.  
  660.  
  661. WHAT SHOULD YOU DO NOW?
  662. _________________________________________________________________
  663.  
  664. You have reached a major point in your excursion of object oriented
  665. programming, because you now have most of the knowledge you need
  666. to do some serious object oriented programming.  The best thing for
  667. you to do at this point is stop studying and get busy programming,
  668. using some of these techniques for your projects.  The only topic
  669. left is the use of virtual methods and you can easily defer its use
  670. for a long time.
  671.  
  672. One point should be made before you begin a serious programming
  673. project.  Your first program could have too many objects and be
  674. nearly unreadable unless you strive to use only a few objects. 
  675. After you gain experience, you can confidently use more objects
  676. with each programming project.  For your first project, define only
  677. a few objects and write the majority of the program in standard
  678. procedural programming methods.  Add a few more objects to your
  679. next project and, as you gain experience, you will feel very
  680. comfortable with the use of objects and your programming methods
  681. will be very clear.
  682.  
  683. Now is the time to begin using this new knowledge but be sure you
  684. enter the water slowly the first time.
  685.  
  686.  
  687. PROGRAMMING EXERCISES
  688. _________________________________________________________________
  689.  
  690. 1.   Modify ENCAP2.PAS in such a way to multiply the height of the
  691.      short pole times the length of the medium box and print the
  692.      result out.  Even though this is possible to do, it requires
  693.      you to expend a bit of effort to accomplish.  Remember that
  694.      you should not use the components of an object directly, only
  695.      through use of the available methods.
  696.  
  697. 2.   Add an object named Pick_Up to INHERIT2.PAS of type Truck and
  698.      initialize it to some reasonable values.  Print out its
  699.      loading and efficiency in a manner similar to the Semi.
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.                                                        page 14-12
  708.